<template>
  <div v-loading="loading" element-loading-text="加载中..." class="container">
    <div class="app-container">
      <el-tree :data="treeData" :props="defaultProps" default-expand-all :expand-on-click-node="false">
        <span slot-scope="{ node , data }" class="custom-tree-node">
          <span>{{ node.label }}</span>
          <!-- manageData插槽 -->
          <manageData :id="data.id">
            <template #name>
              {{ data.managerName }}
            </template>
          </manageData>
        </span>
      </el-tree>
    </div>
  </div>
</template>

<script>
import manageData from './components/manageData.vue'
import { getDepartment } from '@/api/department.js'
import { transListToTreeData } from '@/utils/index.js'
export default {
  name: 'Department',
  components: {
    manageData
  },
  data() {
    return {
      loading: false,
      treeData: [], // 处理后的数据
      defaultData: [], // 未处理的数据
      defaultProps: {
        label: 'name', // 显示的字段名
        children: 'children' // 子节点的字段名
      }
    }
  },
  created() {
    this.GetDepartment()
  },
  methods: {
    // 初始化函数
    async GetDepartment() {
      this.loading = true
      const data = await getDepartment()
      this.defaultData = data
      console.log(this.defaultData)
      this.treeData = transListToTreeData(data, 0) // 处理得到的数据
      this.loading = false
    },
    SenddefaultData() {
      return this.defaultData
    }
  },
  provide() {
    return {
      GetDepartment: this.GetDepartment,
      DefaultData: this.SenddefaultData
    }
  }
}
</script>

<style scoped lang="scss">
.app-container{
 padding: 30px 100px ;
 font-size: 14px;
 .custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    padding-right: 8px;

  }

}
</style>
